/*
 Copyright 2007 Ole Rogeberg

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package org.lifeyears.web.action;

import org.lifeyears.model.User;
import org.lifeyears.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Action for the signup-page.
 *
 * @author Aleksander Blomskøld
 */
@Scope( "prototype" )
@Component
public class SignupAction
    extends AbstractLifeyearsAction
{
    private final User user = new User();

    private String register;

    private String password;

    private String confirmPassword;

    @Autowired
    private UserService userService;

    @Override
    public String execute()
    {
        //check if a user already exists with the given email
        final User oldUser = userService.getUserByEmail( user.getEmail() );

        if (oldUser != null) {
            addActionError( getText("errors.existing.user", new String[] {user.getEmail()}) );
            return ERROR;
        }

        user.hashAndSetPassword( password );
        userService.save( user );
        addActionMessage( getText( "user.registered" ) );

        return SUCCESS;
    }

    public User getUser()
    {
        return user;
    }

    @Override
    public void setUserService( final UserService userService )
    {
        this.userService = userService;
    }

    public void setRegister( final String register )
    {
        this.register = register;
    }

    public void setPassword( final String password )
    {
        this.password = password;
    }

    public void setConfirmPassword( final String confirmPassword )
    {
        this.confirmPassword = confirmPassword;
    }

    public String getPassword()
    {
        return password;
    }

    public String getConfirmPassword()
    {
        return confirmPassword;
    }

}
